package com.guoxue.dao.impl;

import java.io.Serializable;
import java.util.List;


import org.springframework.stereotype.Repository;
import com.guoxue.dao.IUserDao;
import com.guoxue.domain.UserVo;
import com.guoxue.util.codeSwitch.khb;
@Repository("userDaoImpl")
public class UserDaoImpl extends BaseDaoImpl<UserVo> implements IUserDao{
	
	public UserVo getUser(String account) {
		khb.ta.function();//此方法待废弃
		return get(" from UserVo u where u.identityId = ? or u.wechatAccount =? or u.sinaAccount =? or u.qqAccount =? or u.userTel=?", new Object[] {account, account, account, account, account});
	}
	/**
	 * @param account (必)登录账号，快速登录时为手机唯一识别码
	 * @param type (必)1:手机号登录。2、QQ登录.
	 *            3、微信登录.4、sina登录.5、快速登录.
	 */
	public UserVo getUser(String account,int type) {
		
		StringBuffer strBuffer=new StringBuffer();
		String hql="from UserVo u";
		strBuffer.append(hql);
		switch(type){
			case 1:
				strBuffer.append(" where u.userTel=?");
				break;
			case 2:
				strBuffer.append(" where u.qqAccount=?");
				break;
			case 3:
				strBuffer.append(" where u.wechatAccount=?");
				break;
			case 4:
				strBuffer.append(" where u.sinaAccount=?");
				break;
			case 5:
				strBuffer.append(" where u.identityId=?");
				break;
			default:
				break;
		
		}
		return get(strBuffer.toString(), new Object[] {account});
	}
	
	public UserVo getUserById(String identityId) {
		return get("from UserVo u where u.identityId = ?", new Object[] {identityId});
	}
	

	public List<UserVo> getUserList() {
		return find("from UserVo u");
	}
	
	public void saveUser(UserVo user) {
		saveOrUpdate(user);
	}

	public UserVo getUserByTel(String account) {
		return get("from UserVo u where u.userTel = ?", new Object[] {account});
	}

	public UserVo getUserByNickname(String nickName) {
		return get("from UserVo u where u.nickName = ?", new Object[] {nickName});
	}
	
	public int updateUserNickName(String nickName, String account){
		return executeHql("update UserVo u set u.nickName = ? where u.userTel = ? or u.wechatAccount =? "
				+ "or u.sinaAccount = ? or u.qqAccount = ? or u.identityId = ?", new Object[] {nickName,account,account
						,account,account,account});
	}

	public void updateAccountStatus(int id, int status) {
		executeHql("UPDATE UserVo u set u.status=? WHERE u.id=?", new Object[] {status,id});
	}
	@Override
	public UserVo getUserByKey(int id) {
		return get("from UserVo u where u.id = ? and u.sysflag=0", new Object[] {id});
	}


	@Override
	public Serializable saveReturnKey(UserVo entiy) {
		return save(entiy);
	}


	@Override
	public UserVo getUserBySina(String userId) {
		return get("FROM UserVo u WHERE u.sinaAccount=? AND u.sysflag=0", new Object[] {userId});
	}


	@Override
	public UserVo getUserByQq(String userid) {
		return get("FROM UserVo u WHERE u.qqAccount=? AND u.sysflag=0", new Object[] {userid});
	}


	@Override
	public List<UserVo> getByAccount(String account) {
		return find("FROM UserVo u WHERE u.userTel=? or u.id=? or u.nickName like ?", new Object[] {account});
	}


	@Override
	public List<UserVo> getByName(int uid,int sysflag ,String name) {
		return find("SELECT u.nickName, u.id,u.imgUrl,u.personSignature,u.sex From UserVo u, FollowsVo f WHERE u.id = f.followed AND f.follower=? AND f.sysflag=? AND u.sysflag=0 AND u.nickName LIKE ?", new Object[]{uid,sysflag,"%"+name+"%"});
	}

	@Override
	public List<UserVo> getUserLikeNickname(String param) {
		return find("From UserVo u WHERE u.nickName LIKE ? AND sysflag=0",new Object[]{"%"+param+"%"});
	}


	@Override
	public List<UserVo> getFensByUid(int uid, String name) {
		return find("SELECT u.nickName, u.id,u.imgUrl,u.personSignature,u.sex From UserVo u, FollowsVo f WHERE u.id = f.follower AND f.followed=? AND f.sysflag=0 AND u.sysflag=0 AND u.nickName LIKE ?", new Object[]{uid,"%"+name+"%"});
	}
	
	
	
}
